# -*- coding: utf-8 -*-
# @Time    : 2024/1/16 21:13
# @Author  : micah
# @File    : 5.4399数据采集并将数据保存到json文件中.py
# @Software: PyCharm


import json
import requests
from lxml import etree


class GetGameInfo:
    game_info_list = list()

    def __init__(self):
        self.url = 'https://www.4399.com/flash/'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        }

    def save_info(self):
        response = requests.get(self.url, headers=self.headers).content.decode('GB2312')

        tree = etree.HTML(response)
        li_list = tree.xpath("//ul[@class='n-game cf']/li")
        for li in li_list:
            item = dict()
            game_url = li.xpath("./a/@href")[0]
            title = li.xpath(".//b/text()")[0]
            item['title'] = title
            item['url'] = game_url
            self.game_info_list.append(item)

        print(self.game_info_list)
        with open('game_info.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(self.game_info_list, indent=4, ensure_ascii=False))


get_game_info = GetGameInfo()
get_game_info.save_info()
